package zq.cn.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import zq.cn.adapter.MyAdapter;
import zq.cn.adapter.MyPagerAdapter;
import zq.cn.utils.BtnTools;
import zq.cn.utils.Globals;
import zq.cn.utils.SqlDao;

public class MainActivity extends BtnTools{
    private MyPagerAdapter adapter;
    private TextView tv;
    private List<View> listView;
    private ViewPager vp;
    private String[] topTitle = new String[]{"程序员面试宝典","全部问题","搜索关键字"};
    private Button setting;
    private PopupWindow pw;
    private Class[] classes = new Class[] {MainActivity.class, ViewInfoActivity.class, SearchActivity.class};
    private Button[] btn = new Button[3];
    private int[] btnColor0 = new int[] {R.drawable.botton_icon01b,R.drawable.botton_icon02b,R.drawable.botton_icon03b};
    private int[] btnColor1 = new int[] {R.drawable.botton_icon01a,R.drawable.botton_icon02a,R.drawable.botton_icon03a};
    private int i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv  = (TextView) findViewById(R.id.top_text);
        this.init(0);

        vp = (ViewPager) findViewById(R.id.pager_vp);
        listView = new ArrayList<>();
        View view1 = LayoutInflater.from(this).inflate(R.layout.pager_main,null);
        View view2 = LayoutInflater.from(this).inflate(R.layout.pager_view_info,null);
        View view3 = LayoutInflater.from(this).inflate(R.layout.pager_search,null);

        listView.add(view1);
        listView.add(view2);
        listView.add(view3);

        this.showText(view2);
        this.searchByKeyword(view3);

        vp.setOffscreenPageLimit(3);
        adapter = new MyPagerAdapter(listView);
        vp.setAdapter(adapter);

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tv.setText(topTitle[position]);
                for(int i=0;i<btn.length;i++) {
                    if(i==position){
                        btn[i].setBackgroundResource(btnColor1[i]);
                    }else {
                        btn[i].setBackgroundResource(btnColor0[i]);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //翻页，页面顶部文字及按钮设置
    public void init(int index){

        btn[0] = (Button) findViewById(R.id.btn_0);
        btn[1] = (Button) findViewById(R.id.btn_1);
        btn[2] = (Button) findViewById(R.id.btn_2);
        for(i=0;i<btn.length;i++) {
            final int temp =i;
            if(i==index) {
                btn[i].setBackgroundResource(btnColor1[i]);
            }else {
                btn[i].setBackgroundResource(btnColor0[i]);
            }
            btn[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    vp.setCurrentItem(temp, true);
                }
            });
        }

        setting = (Button) findViewById(R.id.btn_setting);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pw = new PopupWindow(Globals.SCREEN_WIDTH / 4, Globals.SCREEN_HEIGHT / 6);
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.setting_info, null);
                pw.setContentView(view);
                if (pw.isShowing()) {
                    pw.dismiss();
                } else {
                    pw.showAsDropDown(setting);
                }
//              内部类调用
                TextView version = (TextView) view.findViewById(R.id.win_version);
                TextView about = (TextView) view.findViewById(R.id.win_about);
                TextView exit = (TextView) view.findViewById(R.id.set_exit);

                version.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pw.dismiss();
                        Toast.makeText(MainActivity.this, "版本信息：1.0", Toast.LENGTH_LONG).show();
                    }
                });

                about.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pw.dismiss();
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setTitle("关于我们");
                        builder.setMessage("张强 2016-04-19\r\n" + "导师：孙逊");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        builder.create().show();
                    }
                });

                exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pw.dismiss();
                        finish();
                    }
                });
            }
        });
    }

    //显示文字模块
    private ListView list;
    private MyAdapter adp;
    private List<Map<String, Object>> values;
    private int pagenum =1;
    private int pagesie =10;
    private int recordCound;
    private int pageCount;
    private String keyword;
    int first;
    int visible;
    int total;
    public void showText(View view) {
        list = (ListView) view.findViewById(R.id.list);

        if(keyword==null) {
            keyword ="";
        }

        values = SqlDao.ViewPageData(pagenum, pagesie, keyword);
        recordCound = SqlDao.getCound(keyword);
        pageCount = (recordCound-1)/pagesie+1;

        final TextView text = new TextView(this);
        text.setText("正在刷新。。。。");
        text.setTextColor(Color.RED);
        text.setTextSize(14);
        list.addFooterView(text);

        adp = new MyAdapter(this, values);
        list.setAdapter(adp);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LinearLayout layout = (LinearLayout) view;
//                layout.getChildAt(1).setVisibility(View.VISIBLE);//设置显示属性
                Map<String, Object> map = values.get(position);
                //答案的显示和隐藏
                if (layout.getChildCount() == 1) {
                    //如果得到LinearLayout的TextView总数为1时，加入answer
                    //新建TextView，放入文本
                    TextView answer = new TextView(MainActivity.this);
                    answer.setTextSize(14);
                    answer.setTextColor(Color.RED);
                    answer.setText(map.get("answer").toString());
                    layout.addView(answer);
                    map.put("flag", true);
                } else {
                    //删除answer
                    layout.removeViewAt(1);
                    map.put("flag", false);
                }
            }
        });

        list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(scrollState==SCROLL_STATE_IDLE && first+visible==total && total!=0) {
                    if(pagenum<=pageCount) {
                        pagenum++;
                        values.addAll(SqlDao.ViewPageData(pagenum, pagesie, keyword));
                        adp.notifyDataSetChanged();
                    }else {
                        if(list.getFooterViewsCount()>0) {
                            list.removeFooterView(text);
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                first = firstVisibleItem;
                visible = visibleItemCount;
                total = totalItemCount;
            }
        });
    }

    //关键字查询
    private Button searchBtn;
    private EditText et;
    public void searchByKeyword(View v){
        searchBtn= (Button) v.findViewById(R.id.search_btn);
        et = (EditText) v.findViewById(R.id.et);

        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                keyword = et.getText().toString();
                pagenum=1;
                values.clear();
                values.addAll(SqlDao.ViewPageData(pagenum, pagesie, keyword));
                adp.notifyDataSetChanged();
                vp.setCurrentItem(1,true);
            }
        });
    }
}

